﻿#include "pch.h"
#include "TitleBarViewModel.h"
#include "TitleBarViewModel.g.cpp"


namespace winrt::NovelReader::implementation
{
    TitleBarViewModel::TitleBarViewModel()
    {
        m_fileOpenCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnFileOpenCommandInvoked}, nullptr); 
        m_exitCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnExitCommandInvoked}, nullptr); 
        m_setFontSizeCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnSetFontSizeCommandInvoked}, nullptr); 
        m_setFontFamilyCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnSetFontFamilyCommandInvoked}, nullptr); 
        m_setReaderThemeCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnSetReaderThemeCommandInvoked}, nullptr); 
        m_setWhiteLinesCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnSetWhiteLinesCommandInvoked}, nullptr); 
        m_changeScrollDirectionCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnChangeScrollDirectionCommandInvoked}, {this, &TitleBarViewModel::CanScrollDirectionCommandInvoked});
        m_hideContentsCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnHideContentsCommandInvoked},nullptr); 
        m_autoTurnPageCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnAutoTurnPageCommandInvoked}, nullptr); 
        m_setAutoTurnPageSpeedCommand = winrt::NovelReader::DelegateCommand({this, &TitleBarViewModel::OnSetAutoTurnPageSpeedCommandInvoked}, nullptr); 

        m_halfTitleVisibility = winrt::Microsoft::UI::Xaml::Visibility::Visible;
        m_fullTitleVisibility = winrt::Microsoft::UI::Xaml::Visibility::Collapsed; 
        m_autoTurnPageMenuItemText = L"开始自动翻页"; 
    }

    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::FileOpenCommand()
    {
        return m_fileOpenCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::ExitCommand()
    {
        return m_exitCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::SetFontSizeCommand()
    {
        return m_setFontSizeCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::SetFontFamilyCommand()
    {
        return m_setFontFamilyCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::SetReaderThemeCommand()
    {
        return m_setReaderThemeCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::SetWhiteLinesCommand()
    {
        return m_setWhiteLinesCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::ChangeScrollDirectionCommand()
    {
        return m_changeScrollDirectionCommand;
    }
    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::HideContentsCommand()
    {
        return m_hideContentsCommand;
    }

    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::AutoTurnPageCommand()
    {
        return m_autoTurnPageCommand;
    }

    winrt::Microsoft::UI::Xaml::Input::ICommand TitleBarViewModel::SetAutoTurnPageSpeed()
    {
        return m_setAutoTurnPageSpeedCommand;
    }

    winrt::hstring TitleBarViewModel::DisplayTitle()
    {
        return m_displayTitle;
    }

    void TitleBarViewModel::SetDisplayTitle(winrt::hstring const& value)
    {
        if (value != m_displayTitle) {
            m_displayTitle = value; 
            this->RaisePropertyChanged(L"DisplayTitle"); 
        }
    }

    winrt::hstring TitleBarViewModel::TrunPageMenuItemText()
    {
        return m_autoTurnPageMenuItemText; 
    }

    void TitleBarViewModel::TrunPageMenuItemText(winrt::hstring const& value)
    {
        if (value != m_autoTurnPageMenuItemText) {
            m_autoTurnPageMenuItemText = value; 
            this->RaisePropertyChanged(L"TrunPageMenuItemText"); 
        }
    }

    winrt::Microsoft::UI::Xaml::Visibility TitleBarViewModel::HalfTitleVisibility() const
    {
        return m_halfTitleVisibility;
    }

    void TitleBarViewModel::HalfTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility const& value)
    {
        if (value != m_halfTitleVisibility) {
            m_halfTitleVisibility = value; 
            this->RaisePropertyChanged(L"HalfTitleVisibility"); 
        }
    }

    winrt::Microsoft::UI::Xaml::Visibility TitleBarViewModel::FullTitleVisibility() const
    {
        return m_fullTitleVisibility;
    }

    void TitleBarViewModel::FullTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility const& value)
    {
        if (value != m_fullTitleVisibility) {
            m_fullTitleVisibility = value; 
            this->RaisePropertyChanged(L"FullTitleVisibility");
        }
    }

    winrt::Microsoft::UI::Xaml::Media::Brush TitleBarViewModel::ReadingMenuItemBackground()
    {
        auto color = winrt::Windows::UI::Colors::Transparent(); 
        if (m_enableAutoTurnPage) {
            color = winrt::Windows::UI::Colors::LightGreen(); 
        }
        auto brush = winrt::Microsoft::UI::Xaml::Media::SolidColorBrush(color); 
        return brush;
    }

    winrt::event_token TitleBarViewModel::ScrollDirectionChanged(winrt::NovelReader::ScrollDirectionChangedHandler const& handler)
    {
        return m_scrollDirectionChanged.add(handler); 
    }

    void TitleBarViewModel::ScrollDirectionChanged(winrt::event_token const& token) noexcept
    {
        m_scrollDirectionChanged.remove(token); 
    }

    winrt::event_token TitleBarViewModel::ContentsVisibilityChanged(winrt::NovelReader::VisibilityActionHandler const& handler)
    {
        return m_contentVisibilityChanged.add(handler); 
    }

    void TitleBarViewModel::ContentsVisibilityChanged(winrt::event_token const& token) noexcept
    {
        m_contentVisibilityChanged.remove(token); 
    }

    winrt::event_token TitleBarViewModel::AutoTurnPageEvent(winrt::NovelReader::BooleanActionHandler const& handler)
    {
        return m_autoTrunPageEvent.add(handler); 
    }

    void TitleBarViewModel::AutoTurnPageEvent(winrt::event_token const& token) noexcept
    {
        m_autoTrunPageEvent.remove(token); 
    }

    winrt::fire_and_forget TitleBarViewModel::OnFileOpenCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto windowId = winrt::Microsoft::UI::WindowId{}; 
        windowId.Value = winrt::NovelReader::MainWindow::IdValue(); 

        auto picker = winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker(windowId);
        picker.FileTypeFilter().Append(L".txt"); 
        picker.SuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId::Downloads); 

        auto result = co_await picker.PickSingleFileAsync(); 
        if (result) {
            winrt::NovelReader::AppService::UserSettings().FilePath(result.Path()); 
        }
    }

    void TitleBarViewModel::OnExitCommandInvoked(winrt::Windows::Foundation::IInspectable const&)
    {
        winrt::Microsoft::UI::Xaml::Application::Current().Exit(); 
    }

    void TitleBarViewModel::OnSetFontSizeCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto delta = winrt::unbox_value<double>(parameter); 
        auto newFontSize = max(5.0, delta + winrt::NovelReader::AppService::UserSettings().FontSize()); 
        winrt::NovelReader::AppService::UserSettings().FontSize(newFontSize);
    }

    void TitleBarViewModel::OnSetFontFamilyCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto fontName = winrt::unbox_value<winrt::hstring>(parameter); 
        winrt::NovelReader::AppService::UserSettings().FontName(fontName); 
    }

    void TitleBarViewModel::OnSetReaderThemeCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto themeIndex = winrt::unbox_value<int32_t>(parameter); 
        winrt::NovelReader::AppService::UserSettings().ThemeIndex(themeIndex); 
    }

    void TitleBarViewModel::OnSetWhiteLinesCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto whiteLines = winrt::unbox_value<int32_t>(parameter); 
        winrt::NovelReader::AppService::UserSettings().WhiteLines(whiteLines); 
    }

    void TitleBarViewModel::OnChangeScrollDirectionCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto value = winrt::unbox_value<int32_t>(parameter); 
        auto direction = (winrt::NovelReader::ScrollDirection)value; 
        m_scrollDirectionChanged(direction); 
    }

    void TitleBarViewModel::OnHideContentsCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto isHidden = winrt::unbox_value<bool>(parameter); 

        if (isHidden) {
            this->HalfTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility::Collapsed); 
            this->FullTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility::Visible); 
            m_contentVisibilityChanged(winrt::Microsoft::UI::Xaml::Visibility::Collapsed); 
        }
        else {
            this->HalfTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility::Visible);
            this->FullTitleVisibility(winrt::Microsoft::UI::Xaml::Visibility::Collapsed);
            m_contentVisibilityChanged(winrt::Microsoft::UI::Xaml::Visibility::Visible); 
        }
    }

    void TitleBarViewModel::OnAutoTurnPageCommandInvoked(winrt::Windows::Foundation::IInspectable const&)
    {
        m_enableAutoTurnPage = !m_enableAutoTurnPage; 
        if (m_enableAutoTurnPage) {
            this->TrunPageMenuItemText(L"暂停自动翻页");
        }
        else {
            this->TrunPageMenuItemText(L"开始自动翻页"); 
        }
        m_autoTrunPageEvent(m_enableAutoTurnPage); 
        this->RaisePropertyChanged(L"ReadingMenuItemBackground");
    }

    void TitleBarViewModel::OnSetAutoTurnPageSpeedCommandInvoked(winrt::Windows::Foundation::IInspectable const& parameter)
    {
        auto speed = winrt::unbox_value<int32_t>(parameter); 
        winrt::NovelReader::AppService::UserSettings().AutoTurnPageSpeed(speed); 
    }

    bool TitleBarViewModel::CanScrollDirectionCommandInvoked(winrt::Windows::Foundation::IInspectable const&) const
    {
        return !m_enableAutoTurnPage; 
    }
}
